//
//  RTJAutoBorrowingController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/20.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJAutoBorrowingController.h"
#import "RTJAutoMarkCell.h"
#import "RTJCapitalSubsidiaryCell.h"

@interface RTJAutoBorrowingController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *selectedData;
@property (nonatomic, strong) NSMutableArray *indexArr;

@end

@implementation RTJAutoBorrowingController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.tableView.estimatedRowHeight=300;
    
    _selectedData = [NSMutableArray array];
    _indexArr = [NSMutableArray array];
    
    if ([_data[@"deal_list"] count] == 0) {
        
        [self.view showEmptyViewWithStr:@"暂无数据" image:[UIImage imageNamed:@"kbylogo"] callback:^{
            
            [self.tableView reloadData];
            
        }];
        
    }else{
        
        [self.view removeEmptyView];
        [self.tableView reloadData];
        
    }

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [_data[@"deal_list"] count];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJCapitalSubsidiaryCell";
    
    RTJCapitalSubsidiaryCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"RTJCapitalSubsidiaryCell" owner:self options:nil][3];
    }
    
    NSString *index = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    
    if (![self.indexArr containsObject:index]) {
        
        cell.isOpen = YES;
        
    }else{
        
        for (NSDictionary *dic in self.selectedData) {
            
            if([[dic allKeys] containsObject:index]) {
                
                NSString *isOpen = [NSString stringWithFormat:@"%@",[dic valueForKey:index]];
                
                if ([isOpen isEqualToString:@"1"]) {
                    
                    cell.isOpen = NO;
                    
                }else{
                    
                    cell.isOpen = YES;
                    
                }
                
            }
        }
        
    }
    
    cell.jiekuandata = _data[@"deal_list"][indexPath.row];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return  cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.selectedData.count == 0) {
        
        NSString *index = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
        NSString *str = @"1";
        NSDictionary *dic = @{index:str};
        [self.selectedData addObject:dic];
        [self.indexArr addObject:index];
        
        NSIndexPath *indexPaths=[NSIndexPath indexPathForRow:indexPath.row inSection:0];
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPaths,nil] withRowAnimation:UITableViewRowAnimationNone];
        
    }else{
        
        NSString *index = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
        
        if (![self.indexArr containsObject:index]) {
            
            [self.indexArr addObject:index];
            
            NSString *str = @"1";
            NSDictionary *dic = @{index:str};
            [self.selectedData addObject:dic];
            
            NSIndexPath *indexPaths=[NSIndexPath indexPathForRow:indexPath.row inSection:0];
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPaths,nil] withRowAnimation:UITableViewRowAnimationNone];
            
        }else{
            
            [self.selectedData enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                
                if ([[obj allKeys] containsObject:index]) {
                    
                    NSString *isOpen1 = [NSString stringWithFormat:@"%@",[obj valueForKey:index]];
                    
                    if ([isOpen1 isEqualToString:@"0"]) {
                        
                        NSDictionary *dic = @{index:@"1"};
                        [self.selectedData replaceObjectAtIndex:idx withObject:dic];
                        
                        NSIndexPath *indexPaths=[NSIndexPath indexPathForRow:indexPath.row inSection:0];
                        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPaths,nil] withRowAnimation:UITableViewRowAnimationNone];
                        
                    }else{
                        
                        NSDictionary *dic = @{index:@"0"};
                        [self.selectedData replaceObjectAtIndex:idx withObject:dic];
                        
                        NSIndexPath *indexPaths=[NSIndexPath indexPathForRow:indexPath.row inSection:0];
                        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPaths,nil] withRowAnimation:UITableViewRowAnimationNone];
                        
                    }
                    
                }
                
            }];
        }
    }
    
}


@end
